// pages/doctor-my-data-rz/doctor-my-data-rz.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorId: '',
    imgUrl: app.globalData.imgUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadings();
  },

  //获取医生个人信息
  loadings: function() {
    var doctorId = wx.getStorageSync('userid');
    var that = this;
    console.log('doctorId', doctorId);
    wx.request({
      url: app.globalData.baseUrl + 'doctor/doctor/doctorCardInfo',
      data: {
        'doctorId': doctorId
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('获取医生个人信息:', res.data.data)
        wx.hideLoading();
        that.setData({
          picture: res.data.data[0],
          cardFront: res.data.data[0].cardFront,
          cardOpposite: res.data.data[0].cardOpposite,
          //用来以后对比路径是否对
          cardFronts: res.data.data[0].cardFront,
          cardOpposites: res.data.data[0].cardOpposite,
        })
      }
    })
  },
  //保存
  nextstep: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '修改身份证信息需要重新审核！',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.present();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //编辑身份证
  present: function() {
    var that = this;
    var cardFront = that.data.cardFront;
    var cardOpposite = that.data.cardOpposite;
    var cardFronts = that.data.cardFronts;
    var cardOpposites = that.data.cardOpposites;
    console.log("cardFront", cardFront);
    console.log("cardOpposite", cardOpposite);
    console.log("cardFronts", cardFronts);
    console.log("cardOpposites", cardOpposites);
    if (String(cardFront) == String(cardFronts)) {
      wx.showToast({
        title: '请更改身份证正面照片',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (String(cardOpposite) == String(cardOpposites)) {
      wx.showToast({
        title: '请更改身份证背面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    wx.request({
      url: app.globalData.baseUrl + 'doctor/doctor/editCardInfo',
      data: {
        'cardFront': cardFront,
        'cardOpposite': cardOpposite,
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('编辑资质信息:', res.data)
        if (res.data.status_code == 200) {
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/doctor-login/doctor-login',
            })
          }, 2000)
        }
      }
    })
  },
  /**
   * loading加载
   */
  showLoading: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  },
  // 身份证正面上传图片
  cardFront: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('身份证正面上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardFront: path
            })
            // that.present();
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 身份证背面上传图片
  cardOpposite: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('身份证背面上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardOpposite: path
            })
            // that.present();
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})